//  MayBe 函子

class MayBe { 
  static of(val) { 
    return new MayBe(val)
  }
  constructor(val) { 
    this._val = val
  }
  map(fn) { 
    return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._val))
  }

  isNothing() { 
    return this._val === null || this._val === undefined
  }
}

let r = MayBe.of('hello world')
  .map(x => x.toUpperCase())

  console.log(r); // MayBe { _val: 'HELLO WORLD' }
  

let n = MayBe.of(null)
  .map(x => x.toUpperCase())

console.log(n); // MayBe { _val: null }
  
let x = MayBe.of(null)
  .map(x => x.toUpperCase())
  .map(x => null)
  .map(x => x.splice(' '))

  console.log(x); // MayBe { _val: null } 并不知道是那个位置出了差错 either 函子可解决